package com.resto.scm.web.service.impl;


import cn.restoplus.common.utils.log.LogBetter;
import cn.restoplus.common.utils.log.LogLevel;
import cn.restoplus.rpc.server.RpcService;
import com.resto.common.dao.domain.BaseQuery;
import com.resto.common.dao.domain.PageResult;
import com.resto.common.dao.domain.UpdateByQuery;
import com.resto.scm.web.dto.CategoryOne;
import com.resto.scm.web.dto.CategoryThree;
import com.resto.scm.web.dto.CategoryTwo;
import com.resto.scm.web.manager.MdCategoryManager;
import com.resto.scm.web.model.MdCategory;
import com.resto.scm.web.service.CategoryService;
import com.resto.scm.web.util.SerialNumberUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import static cn.restoplus.common.tracelog.TraceLoggerFactory.logger;
/**
 *
 * 类别管理
 */

@RpcService
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private MdCategoryManager mdCategoryManager;



    @Override
    public Integer addCategory(MdCategory mdCategory) {
        mdCategory.setGmtCreate(new Date());
        //todo 实现分类编码按分类递增
        MdCategory parentCategory = queryById(mdCategory.getParentId());
        if (parentCategory == null){
            LogBetter.instance(logger)
                    .setLevel(LogLevel.INFO)
                    .setMsg("通过该parentId找不到上一级分类")
                    .addParm("parentId:", mdCategory.getParentId())
                    .log();
        }else if (StringUtils.isBlank(parentCategory.getCategoryCode())){
            LogBetter.instance(logger)
                    .setLevel(LogLevel.INFO)
                    .setMsg("通过该parentId找不到上一级分类parent categoryCode为空")
                    .log();

        }else {
            mdCategory.setCategoryCode(SerialNumberUtil.getCategoryCode(mdCategory.getCategoryHierarchy()
                    ,parentCategory.getCategoryCode(),mdCategory.getBrandId()));
        }
        if (mdCategory.getCategoryHierarchy() == 1 || mdCategory.getCategoryHierarchy() == 2){
            mdCategory.setShopDetailId(null);
        }
        return mdCategoryManager.insert(mdCategory);
    }


    @Override
    public PageResult<MdCategory> query4Page(MdCategory mdCategory) {
        return null;
    }

    @Override
    public MdCategory queryById(Long id) {
        return mdCategoryManager.getById(id);
    }

    @Override
    public List<MdCategory> queryByParentId(Long parentId) {
        MdCategory mdCategory = new MdCategory();
        mdCategory.setParentId(parentId);
        BaseQuery<MdCategory> o =BaseQuery.getInstance(mdCategory);
        return mdCategoryManager.query(o);

    }

    @Override
    public Integer updateMdCategory(MdCategory mdCategory){
        mdCategory.setGmtModified(new Date());
        BaseQuery<MdCategory> mm= BaseQuery.getInstance(new MdCategory());
        mm.getData().setId(mdCategory.getId());
        UpdateByQuery<MdCategory> o = new UpdateByQuery<>(mm,mdCategory);
        return mdCategoryManager.updateByQuery(o);
    }

    @Override
    public Integer deleteById(Long id) {
        MdCategory mdCategory = new MdCategory();
        mdCategory.setIsDelete(1);
        mdCategory.setGmtModified(new Date());

        BaseQuery<MdCategory> mm= BaseQuery.getInstance(new MdCategory());
        mm.getData().setId(id);

        UpdateByQuery<MdCategory> o = new UpdateByQuery<>(mm,mdCategory);
        return mdCategoryManager.updateByQuery(o);

    }

    @Override
    public List<MdCategory> queryByCategoryHierarchy(Integer categoryHierarchy,String shopId) {
        MdCategory mdCategory = new MdCategory();
        mdCategory.setCategoryHierarchy(categoryHierarchy);
        mdCategory.setShopDetailId(shopId);
        BaseQuery<MdCategory> o =BaseQuery.getInstance(mdCategory);
        return mdCategoryManager.query(o);
    }

    @Override
    public List<MdCategory> queryDown(Integer categoryHierarchy, Long id,String shopId) {
        MdCategory mdCategory = new MdCategory();
        mdCategory.setCategoryHierarchy(categoryHierarchy);
        mdCategory.setParentId(id);
        mdCategory.setShopDetailId(shopId);
        BaseQuery<MdCategory> o =BaseQuery.getInstance(mdCategory);
        return mdCategoryManager.query(o);
    }

    @Override
    public List<CategoryOne> queryAll(String shopId) {
        List<CategoryOne> list = mdCategoryManager.findCategoryList();
        for (CategoryOne one : list){
            for (CategoryTwo two : one.getTwoList()){
                two.setThreeList(mdCategoryManager.findCategoryThreeList(two.getId(),shopId));
            }
        }
        return list;
    }

    @Override
    public List<MdCategory> queryCategories() {
        return mdCategoryManager.findCategories();
    }

}
